<?php

namespace App\Services;

use App\Models\AqTool;
use Slowlyo\OwlAdmin\Services\AdminService;

/**
 * 集成工具
 *
 * @method AqTool getModel()
 * @method AqTool|\Illuminate\Database\Query\Builder query()
 */
class AqToolService extends AdminService
{
	protected string $modelName = AqTool::class;

	public function searchable($query)
	{
		parent::searchable($query);

		$query->when($this->request->input('keywords'), fn($q) => $q->where('title', 'like', '%' . $this->request->input('keywords') . '%'));
		$query->when($this->request->input('keywords'), fn($q) => $q->where('name', 'like', '%' . $this->request->input('keywords') . '%'));
	}

	public function sortable($query)
	{
		if (request()->orderBy && request()->orderDir) {
			$query->orderBy(request()->orderBy, request()->orderDir ?? 'asc');
		} else {
			$query->orderBy('custom_order');
		}
	}

	public function saved($model, $isEdit = false)
	{
		cache()->forget('aq:tools');
		AqService::make()->getTools();
	}

}
